using System;
using System.Globalization;
using System.Web.Mvc;

namespace Chatu.ControlLider.Web.Models.Common
{
    public static class HtmlExtensions
    {
        public static string ToFullDateTimeSpanish(this DateTime date)
        {
            return string.Format("{0}, {1} de {2} de {3} - {4}",
                ToPascalCasing(date.ToString("dddd")), 
                date.Day,
                ToPascalCasing(date.ToString("MMMM")), 
                date.Year, 
                date.ToString("hh:mm tt", CultureInfo.CreateSpecificCulture("en-US")));
        }

        public static string ToPascalCasing(string input)
        {
            return input.Substring(0, 1).ToUpper() + input.Substring(1);
        }

        public static string ToGuardiaValue(this double? guardia)
        {
            if (guardia.HasValue)
            {
                string value = guardia.Value.ToString();
                string[] values = value.Split(new char[] { ',' });
                
                if (values.Length > 1)
                {
                    if (values[1] == "5")
                        return string.Format("{0} 1/2", values[0]);
                }

                return value;
            }

            return null;
        }

        public static string ToBonoValue(this double? bono)
        {
            if (bono.HasValue)
            {
                string value = bono.Value.ToString();
                string[] values = value.Split(new char[] { ',' });
                string min = "00";

                if (values.Length > 1 && values[1].Length > 0)
                    min = values[1].Length == 1 ? string.Format("{0}0", values[1]) : values[1];
                
                return string.Format("{0}:{1} Hora{2}", values[0], min, values[0] == "1" ? "" : "s");
            }

            return null;
        }

        public static string SubmitButton(this HtmlHelper html, string name, string value)
        {
            return SubmitButton(html, name, value, false, null);
        }

        public static string SubmitButton(this HtmlHelper html, string name, string value, bool disabled, string onClick)
        {
            TagBuilder tag = new TagBuilder("input");

            tag.MergeAttribute("id", name);
            tag.MergeAttribute("name", name);
            tag.MergeAttribute("value", value);
            tag.MergeAttribute("type", "submit");

            if (disabled)
                tag.MergeAttribute("disabled", "disabled");
            else if (!string.IsNullOrEmpty(onClick))
                tag.MergeAttribute("onclick", onClick);

            return tag.ToString(TagRenderMode.SelfClosing);
        }
    }
}